package Queue;

import Stack.MyStack;

public class MyQueue {
//头进尾出
    static class ListNode {
        private int val;
        private ListNode next;
        private ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode front;
    private ListNode rear;
    private int usedSize;

    public void offer(int val) {
        ListNode node = new ListNode(val);
        if(front==null) {
            front=rear=node;
        }else {
            node.next = front;
            front.prev = node;
            front = node;
        }
        usedSize++;
    }

    public int poll() {
        if(rear==null) {
            return -1;
        }
        int data = rear.val;
        if(rear==front) {
            rear = null;
            front = null;
        }
        rear = rear.prev;
        rear.next = null;
        usedSize--;
        return data;
    }

    public int peek() {
        int data = rear.val;
        return data;
    }

    public int size() {
        return usedSize;
    }

    public boolean isEmpty() {
        return usedSize==0;
    }

}
class test {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(12);
        myQueue.offer(23);
        myQueue.offer(34);
        myQueue.offer(45);
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.peek());
        System.out.println(myQueue.peek());
        System.out.println(myQueue.size());
    }
}
